package com.miguo.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.miguo.app.AppHelper;
import com.miguo.bean.AdvertBean;
import com.miguo.framework.adapter.RecyclingPagerAdapter;
import com.miguo.utils.Options;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.LinkedList;
import java.util.List;

public class HomeAdSliderAdapter extends RecyclingPagerAdapter {

    private Activity mActivity;
    private LinkedList<AdvertBean> mList;

    public HomeAdSliderAdapter(Activity activity) {
        this.mActivity = activity;
        this.mList = new LinkedList<AdvertBean>();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup container) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            holder.imageView = new ImageView(mActivity);
            holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
            holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            convertView = holder.imageView;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(mList.get(position).getHorizontal_pic_url(), holder.imageView,
                Options.getDefaultOptions());
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.startActivityByType(mActivity, mList.get(position));
            }
        });
        return convertView;
    }

    public void setAdverList(List<AdvertBean> list) {
        this.mList.clear();
        this.mList.addAll(list);
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    private static class ViewHolder {
        ImageView imageView;
    }
}
